package com.wy.springbootblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wy.springbootblog.mapper.TagMapper;
import com.wy.springbootblog.pojo.Category;
import com.wy.springbootblog.pojo.Result;
import com.wy.springbootblog.pojo.SearchBean;
import com.wy.springbootblog.pojo.Tag;
import com.wy.springbootblog.service.TagService;
import com.wy.springbootblog.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author work
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private TagMapper tagMapper;

    // 新增标签
    @Override
    public Result<Void> saveTag(Tag tag) {
        // 1.判断用户是否有操作权限
        Map<String,Object> map = ThreadLocalUtil.get();
        String role = (String) map.get("role");
        if (!Objects.equals(role, "admin")) {
            // 没有新增标签权限
            return Result.error("该用户无新增标签权限");
        }
        // 2.查询标签是否已经存在
        boolean bool = tagMapper.exists(new LambdaQueryWrapper<Tag>().eq(Tag::getName,tag.getName()));
        if (bool) {
            return Result.error("标签已存在");
        }
        // 3.新增标签
        tag.setCreateTime(LocalDateTime.now());
        tag.setUpdateTime(LocalDateTime.now());
        tagMapper.insert(tag);
        return Result.success();
    }

    // 删除标签
    @Override
    public Result<Void> deleteTag(Integer id) {
        // 1.判断用户是否有操作权限
        Map<String,Object> map = ThreadLocalUtil.get();
        String role = (String) map.get("role");
        if (!Objects.equals(role, "admin")) {
            // 没有删除标签权限
            return Result.error("该用户无删除标签权限");
        }
        // 2.查询标签是否已经存在
        boolean bool = tagMapper.exists(new LambdaQueryWrapper<Tag>().eq(Tag::getId,id));
        if (!bool) {
            return Result.error("标签不存在");
        }
        // 3.删除标签
        tagMapper.deleteById(id);
        return Result.success();
    }

    // 分页查询标签
    @Override
    public Result<List<Tag>> tagList(SearchBean<Tag> searchBean) {
        // 分页查询
        Page<Tag> page = new Page<Tag>(searchBean.getPageNum(), searchBean.getPageSize());
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<Tag>();
        queryWrapper.eq("flag", 0);
        // 模糊查询标签name
        if (searchBean.getQuery() != null && searchBean.getQuery().getName() != null) {
            queryWrapper.like("name", searchBean.getQuery().getName());
        }
        tagMapper.selectPage(page, queryWrapper);
        return Result.success(page.getRecords(), page.getTotal());
    }

    // 更新标签
    @Override
    public Result<Void> updateTag(Tag tag) {
        // 1.判断用户身份权限
        Map<String, Object> map = ThreadLocalUtil.get();
        String role = (String) map.get("role");
        if (!Objects.equals(role, "admin")) {
            // 没有修改标签权限
            return Result.error("该用户无修改标签权限");
        }
        // 2.判断标签是否存在
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<Tag>();
        queryWrapper.eq(Tag::getId, tag.getId());
        queryWrapper.eq(Tag::getFlag, 0);
        Tag tagQuery = tagMapper.selectOne(queryWrapper);
        if (tagQuery == null) {
            return Result.error("标签不存在");
        }
        // 3.更新标签
        tag.setUpdateTime(LocalDateTime.now());
        tagMapper.updateById(tag);
        return Result.success();
    }
}
